package org.qloudgen.borg.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Check;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.CheckGroupSelector;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.qloudgen.borg.beans.AppBean;
import org.qloudgen.borg.beans.Cluster;
import org.qloudgen.borg.metadb.DefaultImpl;
import org.qloudgen.borg.pages.common.BasePage;
import org.qloudgen.borg.pages.compnent.CancelButton;

import com.google.common.collect.Lists;

@SuppressWarnings({"unchecked","rawtypes"})
public class AppCreatePage extends BasePage {
	
	/**
	 * @Author youngeryoyo
	 * youngeryoyo@gmail.com
	 */
	static Log log = LogFactory.getLog(AppCreatePage.class);
	
	private static final long serialVersionUID = 1L;
	private transient AppBean  app;
	private transient List<Cluster> clusters = Lists.newArrayList();
	
	public AppCreatePage(PageParameters pageParameters){
		super();
		
		final CheckGroup<Cluster> group = new CheckGroup<Cluster>("group", new ArrayList<Cluster>());
		group.add(new CheckGroupSelector("groupselector"));
		
		// get userno's clusters from metadb
		DefaultImpl.getClusterList(clusters);
		
		// form
		final Form<Object> form = new Form("form",new CompoundPropertyModel<AppBean>(new AppBean())) {
			
			private static final long serialVersionUID = 1L;
			protected void onSubmit() {
				
				List<Cluster> selectClusters = (List<Cluster>) group.getDefaultModelObject();
				app = (AppBean)getDefaultModelObject();
				
				// check cluster info 
				String checkInfo = app.checkAppInfo();
				
				if (StringUtils.isNotEmpty(checkInfo)) { 
					info(checkInfo);
				} else {
					
					app.setAppNo(String.valueOf(DefaultImpl.getAppNo()));
					app.setClusters(selectClusters);
				    
					DefaultImpl.createApp(app);
					setResponsePage(AppListPage.class);
				
				}
			}
			
		};
		this.add(form);
		
		form.add(new RequiredTextField<String>("appName"));
		form.add(new TextField<String>("appDesc"));

		final PageableListView<Cluster> clusterListView =new PageableListView<Cluster>( "clusterRows", clusters, 10) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Cluster> item) {
				final Cluster cluster = (Cluster) item.getModelObject();				
				item.add( new Check("check", item.getModel()));
				item.add( new Label( "clusterID", cluster.getClusterID()));
				item.add( new Label( "clusterName", cluster.getClusterName()));
			}

		};
		group.add(clusterListView);
		form.add(group);
		
		form.add(new Button ("submit"));
		form.add(new CancelButton("cancel",AppListPage.class,pp));
			
	}

}